<template>
  <div class='searchResult-container'>
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad"
      :error.sync="error" error-text="请求失败，点击重新加载">
      <van-cell v-for="(article, index) in list" :key="index" :title="article.title" />
    </van-list>
  </div>
</template>
<script>
import { getSearchResult } from '@/api/search.js';
export default {
  name: 'searchResultPage',
  components: {},
  props: {
    searchText: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      error: false,
      page: '', // 页码
      per_page: 10, // 每页数量
    };
  },
  created() {},
  computed: {},
  methods: {
    onLoad() {
      // 1.获取数据
      getSearchResult({
        page: this.page,
        per_page: this.per_page,
        q: this.searchText,
      })
        .then((res) => {
          console.log(res);
          const { results } = res;
          // 2.将数据添加到数组列表中
          this.list.push(...results);
          // 3.加载完成将Loading置为false
          this.loading = false;
          // 4.判断是否加载结束
          if (results.length) {
            this.page++;
          } else {
            this.finished = true;
          }
        })
        .catch((err) => {
          console.log(err);
          // 加载失败 展示错误提示
          this.loading = false;
          this.error = true;
        });
    },
  },
};
</script>
<style lang='less' scoped>
</style>
